﻿using itk.simple;
using static System.Net.Mime.MediaTypeNames;

namespace ImageDomain.Utils;

/// <summary>
/// todo 用fo-dicom重构
/// </summary>
public class TagUtil
{
    #region Singleton 懒加载单例模式

    private TagUtil() { }
    private static readonly Lazy<TagUtil> Lazy = new(() => new TagUtil());
    public static TagUtil Instance => Lazy.Value;

    #endregion

    /// <summary>
    /// 获取所有Tag
    /// </summary>
    /// <param name="dcmFile"></param>
    /// <returns></returns>
    public Dictionary<string, string> GetAllTags(string dcmFile)
    {
        var itkImage = SimpleITK.ReadImage(dcmFile, PixelIDValueEnum.sitkInt32);
        var tagDic = new Dictionary<string, string>();
        var keys = itkImage.GetMetaDataKeys();

        foreach (var key in keys)
        {
            tagDic.Add(key, itkImage.GetMetaData(key));
        }
        return tagDic;
    }

    public async Task<Dictionary<string, string>> GetAllTagsAsync(string dcmFile) => await Task.Run(() => GetAllTags(dcmFile));

    /// <summary>
    /// 
    /// </summary>
    /// <param name="itkImage"></param>
    /// <param name="tagCodes"></param>
    /// <returns></returns>
    public Dictionary<string, string> GetAllTags(itk.simple.Image itkImage)
    {
        var tagDic = itkImage.GetMetaDataKeys().ToDictionary(key => key, key => itkImage.GetMetaData(key));
        return tagDic;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="itkImage"></param>
    /// <param name="tagCodes"></param>
    /// <returns></returns>
    public async Task<Dictionary<string, string>> GetAllTagsAsync(itk.simple.Image itkImage) => await Task.Run(() => GetAllTags(itkImage));


    /// <summary>
    /// no use
    /// </summary>
    /// <param name="itkImage"></param>
    /// <param name="keyValuePair"></param>
    public void SetTag(itk.simple.Image itkImage, KeyValuePair<string, string> keyValuePair)
    {
        itkImage.SetMetaData(keyValuePair.Key, keyValuePair.Value);
    }

}
